package com.dhcc.sdc.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dto.SdcCdssItemStageDicDto;
import com.dhcc.sdc.cdss.entity.SdcCdssItemStageDicEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssItemStageDicService;
import com.dhcc.sds.base.cache.qcentity.IQcEntityCache;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 诊疗阶段维护
 *
 * @ClassName: SdcTherapyStageController
 * @Description:
 * @author caikunshen
 * @date 2024-06-17
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/sdctherapystage")
public class SdcTherapyStageController extends BaseController {

    private String PREFIX = "/backend/sdc/sdctherapystage/";

    @Autowired
    private ISdcCdssItemStageDicService iSdcCdssItemStageDicService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;

    @RequestMapping("")
    public String index(Model model) {
        //默认加载所有科室
        JSONObject departMap = sdsCommonService.findDeptList();
        //获取病种
        JSONObject entityMap = sdsQcentityService.getQcentity();
        //获取阶段
        JSONObject stageMap = iSdcCdssItemStageDicService.getStage();

        model.addAttribute("departMap", departMap);
        model.addAttribute("entityMap", entityMap);
        model.addAttribute("stageMap", stageMap);

        return PREFIX + "sdctherapystage.html";
    }

    /**
     * 获取列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<SdcCdssItemStageDicDto> page = new PageFactory<SdcCdssItemStageDicDto>().defaultPage();

        if (params.get("stageName") != null && params.get("stageName").equals("全部")) {
            params.put("stageName", "");
        }

        List<SdcCdssItemStageDicDto> result = this.iSdcCdssItemStageDicService.stagePage(page, params);
        page.setRecords((List<SdcCdssItemStageDicDto>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 跳转到新增诊疗阶段字典
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        //获取病种
        JSONObject entityMap = sdsQcentityService.getQcentity();
        model.addAttribute("entityMap",entityMap);
        //获取科室信息
        JSONObject departMap = sdsCommonService.findDeptList();
        model.addAttribute("departMap",departMap);
        return PREFIX + "sdctherapystage_add.html";
    }

    /**
     * 新增诊疗阶段字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(IQcEntityCache.CACHE_NAME)
    public Object add(SdcCdssItemStageDicEntity sdcCdssItemStageDicEntity) {
        if (CommonUtil.isEmpty(sdcCdssItemStageDicEntity.getStageName())) {
            throw new BizException("诊疗阶段名称不能为空");
        }
        if (CommonUtil.isEmpty(sdcCdssItemStageDicEntity.getEntityId())) {
            throw new BizException("所属病种不能为空");
        }
        if (CommonUtil.isEmpty(sdcCdssItemStageDicEntity.getIsActive())) {
            throw new BizException("诊疗阶段状态不能为空");
        }

        sdcCdssItemStageDicEntity.insert();
        return R.ok();
    }

    /**
     * 删除诊疗阶段字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(IQcEntityCache.CACHE_NAME)
    public Object delete(@RequestParam Long sdcCdssItemStageDicId) {
        this.iSdcCdssItemStageDicService.delStageById(sdcCdssItemStageDicId);
        return R.ok();
    }

    /**
     * 跳转到修改诊疗阶段字典
     */
    @RequestMapping("/to_update/{sdcCdssItemStageDicId}")
    public String toUpdate(@PathVariable Long sdcCdssItemStageDicId, Model model) {
        SdcCdssItemStageDicEntity sdcCdssItemStageDic = this.iSdcCdssItemStageDicService.selectById(sdcCdssItemStageDicId);
        model.addAttribute("sdcCdssItemStageDic", sdcCdssItemStageDic);
        // 获取病种
        JSONObject entityMap = sdsQcentityService.getQcentity();
        model.addAttribute("entityMap", entityMap);
        //获取科室信息
        JSONObject departMap = sdsCommonService.findDeptList();
        model.addAttribute("departMap", departMap);
        return PREFIX + "sdctherapystage_edit.html";
    }

    /**
     * 修改诊疗阶段字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @CacheClear(IQcEntityCache.CACHE_NAME)
    public Object update(SdcCdssItemStageDicEntity sdcCdssItemStageDic) {
        if (CommonUtil.isEmpty(sdcCdssItemStageDic.getStageName())) {
            throw new BizException("诊疗阶段名称不能为空");
        }
        if (CommonUtil.isEmpty(sdcCdssItemStageDic.getEntityId())) {
            throw new BizException("所属病种不能为空");
        }
        if (CommonUtil.isEmpty(sdcCdssItemStageDic.getIsActive())) {
            throw new BizException("诊疗阶段状态不能为空");
        }
        if(!this.iSdcCdssItemStageDicService.updateCheck(sdcCdssItemStageDic)) {
            throw new BizException("诊疗阶段下有对应诊疗方案，不能修改所属病种");
        }

        SdcCdssItemStageDicEntity old = this.iSdcCdssItemStageDicService.selectById(sdcCdssItemStageDic.getId());
        old.setEntityId(sdcCdssItemStageDic.getEntityId());
        old.setStageName(sdcCdssItemStageDic.getStageName());
        old.setLinkDept(sdcCdssItemStageDic.getLinkDept());
        old.setIsActive(sdcCdssItemStageDic.getIsActive());
        old.setStageDesc(sdcCdssItemStageDic.getStageDesc());
        old.updateAllColumnById();
        return R.ok();
    }

}
